//
//  GoogleMapTileProvider.m
//  SPMapKit
//
//  Created by zhangbo on 2017/9/7.
//  Copyright © 2017年 zhangbo. All rights reserved.
//

#import "GoogleMapTileProvider.h"

@implementation GoogleMapTileProvider

-(NSString*) tileImageUrl:(NSInteger) row  col:(NSInteger) col level:(NSInteger) level mapType:(SPMapType) mapType
{
    NSString *url = @"";
    switch (mapType) {
        case SPMapTypeStandard:
            url = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=m&gl=CN&x=%ld&y=%ld&z=%ld", row, col, level];
            break;
            
        case SPMapTypeSatellite:
            url = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=s&gl=CN&x=%ld&y=%ld&z=%ld", row, col, level];
            break;
            
        case SPMapTypeHybrid:
            url = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=y&gl=CN&x=%ld&y=%ld&z=%ld", row, col, level];
            break;
            
        default:
            url = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=m&gl=CN&x=%ld&y=%ld&z=%ld", (long)row, (long)col, (long)level];
            break;
    }
    
    return url;
}

-(CGPoint) GPSToContentOffset:(CLLocationCoordinate2D) gpspoint level:(NSInteger) level mapType:(SPMapType) mapType
{
    double mgLat, mgLon;
    convertGPS84_To_Gcj02(gpspoint.latitude, gpspoint.longitude, &mgLat, &mgLon);
    
    double pixelX = 0;
    double pixelY = 0;
    LatLongToPixelXY(mgLat, mgLon, (int)level, &pixelX, &pixelY);
    
    return CGPointMake(pixelX, pixelY);
}

-(CLLocationCoordinate2D) ContentOffsetToGPS:(CGPoint) offset level:(NSInteger) level mapType:(SPMapType) mapType
{
    double lat = 0;
    double lon = 0;
    PixelXYToLatLong(offset.x, offset.y, (int)level, &lat, &lon);
    
    double mgLat, mgLon;
    convertGcj02_To_GPS84(lat, lon, &mgLat, &mgLon);
    
    return CLLocationCoordinate2DMake(mgLat, mgLon);
}

-(CGFloat) GroundResolution:(CGFloat) lat level:(int) level
{
    return GroundResolution(lat, level);
}

-(CGPoint) GPSToTileOffset:(CLLocationCoordinate2D) gpspoint level:(NSInteger) level mapType:(SPMapType) mapType
{
    double mgLat, mgLon;
    convertGPS84_To_Gcj02(gpspoint.latitude, gpspoint.longitude, &mgLat, &mgLon);
    
    double pixelX = 0;
    double pixelY = 0;
    LatLongToPixelXY(mgLat, mgLon, (int)level, &pixelX, &pixelY);
    
    int tileX = 0;
    int tileY = 0;
    PixelXYToTileXY(pixelX, pixelY, &tileX, &tileY);
    
    return CGPointMake(tileX * kDefaultTileSize, tileY * kDefaultTileSize);
}

@end
